package com.igeek.boot.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.anno.WMSLoganno;
import com.igeek.boot.entity.Warehouses;
import com.igeek.boot.result.Result;
import com.igeek.boot.service.IWarehousesService;
import com.igeek.boot.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jwj
 * @since 2023-12-26
 */
@RestController
@RequestMapping("/wisdom_factory/warehouses")
@Api(tags = "仓库表接口")
public class WarehousesController {
    @Autowired
    private IWarehousesService warehousesService;

    //分页查询仓库的数据信息
    @GetMapping("/findPage")
    @ApiOperation("条件分页查询仓库")
    public Result findPage(@ApiParam("分页条件") PageQueryVO pageQueryVO){
        Page<Warehouses> page = warehousesService.findPage(pageQueryVO);
        if(!ObjectUtils.isEmpty(page)) return new Result(true,"分页仓库查询成功",page);
        return new Result(false,"分页仓库查询失败");
    }

    //查询仓库的所有信息
    @GetMapping("/findAll")
    @ApiOperation("查询所有仓库信息")
    public Result findAll(){
        List<Warehouses> list = warehousesService.findAll();
        if(!CollectionUtils.isEmpty(list)) return new Result(true,"仓库信息查询成功",list);
        return new Result(false,"查询失败");
    }

    //将所有坐标存入Redis中
    @GetMapping("/saveAllLocation")
    @ApiOperation("将所有仓库的坐标存入Redis中")
    public Result saveAllLocation(){
        boolean flag = warehousesService.saveAllLocation();
        return new Result(true,"仓库坐标存入Redis成功");
    }

    //添加仓库信息
    @WMSLoganno(module = "仓库",operation = "添加仓库")
    @PreAuthorize("authentication.principal.username == 'admin'")
    @ApiOperation("添加仓库信息")
    @PostMapping("/addWarehouses")
    public Result add(@RequestBody Warehouses warehouses){
        boolean flag = warehousesService.add(warehouses);
        if(!flag) return new Result(false,"仓库信息添加失败");
        return new Result(true,"仓库信息添加成功");
    }

    //删除仓库信息
    @WMSLoganno(module = "仓库",operation = "删除仓库")
    @PreAuthorize("authentication.principal.username == 'admin'")
    @DeleteMapping("/delWarehouses/{id}")
    @ApiOperation("删除仓库信息")
    public Result delWarehouses(@PathVariable("id") Integer id){
        boolean flag = warehousesService.delWarehouses(id);
        return flag?new Result(true,"仓库信息删除成功")
                :new Result(false,"仓库信息删除失败,您当前的仓库可能还有货架没有转移");
    }

    //修改仓库信息
    @WMSLoganno(module = "仓库",operation = "修改仓库")
    @PreAuthorize("authentication.principal.username == 'admin'")
    @PutMapping("/updateWarehouses")
    @ApiOperation("修改仓库信息")
    public Result updateWarehouses(@RequestBody Warehouses warehouses){
        boolean flag = warehousesService.updateWarehouses(warehouses);
        return flag?new Result(true,"仓库信息修改成功")
                :new Result(false,"仓库信息修改失败");
    }

    //查询仓库的详细信息->仓库的基本信息  仓库的货架信息  货架的物资信息
    @GetMapping("/findWarehouses/{id}")
    @ApiOperation("查询仓库的详细信息")
    public Result findWarehouses(@PathVariable("id") Integer id){
        Warehouses warehouses = warehousesService.findOneDetail(id);
        return new Result(true,"仓库信息查询成功",warehouses);
    }

    //根据id查询仓库的location属性
    @GetMapping("/findLocation/{id}")
    @ApiOperation("根据id查询仓库的location属性")
    public Result findLocation(@PathVariable("id") Integer id){
        String location = warehousesService.findOneLocation(id);
        if(StringUtils.hasLength(location))return new Result(true,"仓库信息查询成功",location);
        return new Result(false,"仓库信息查询失败");
    }

    //获取仓库的所有id和point属性
    @GetMapping("/findAllLocationAndName")
    @ApiOperation("查询仓库所有的location属性和name属性")
    public Result findAllLocationAndName(){
        try{
            List<Map<String, Object>> list = warehousesService.findAllLocationAndName();
            if(!CollectionUtils.isEmpty(list)) return new Result(true,"仓库信息查询成功",list);
        }catch(Exception e){
            e.printStackTrace();
        }
        return new Result(false,"仓库信息查询失败");
    }

}
